/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.antexpress.controller;

import com.antexpress.bean.Authentication;
import com.antexpress.common.Const;
import com.antexpress.common.Response;
import com.antexpress.pojo.Bill;
import com.antexpress.pojo.BillSelector;
import com.antexpress.pojo.CustomerInfo;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.MapCity;
import com.antexpress.pojo.MapDistrict;
import com.antexpress.pojo.MapProvince;
import com.antexpress.pojo.MapTown;
import com.antexpress.service.BillService;
import com.antexpress.service.BillServiceImp;
import com.antexpress.util.DataUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Dannis
 */
@Controller
@RequestMapping(value = "/express/")
public class ExpressController {

    @Autowired
    private BillService expressService;

    //获取数据库中省的信息，以便快递员可查询
    @RequestMapping(value = "test.do")
    @ResponseBody
    public void test(@DateTimeFormat(pattern = "yyyy-MM-dd") Date dob) {
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        System.out.println(dob);
    }

    //获取数据库中省的信息，以便快递员可查询
    @RequestMapping(value = "list_working_bills_selector_province.do", method = RequestMethod.POST)
    @ResponseBody
    public Response listWorkingBillsSelector_province(HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.getAllProvince();
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    //获取数据库中市的信息，以便快递员可查询
    @RequestMapping(value = "list_working_bills_selector_city.do", method = RequestMethod.POST)
    @ResponseBody
    public Response listWorkingBillsSelector_city(String selectprovince, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.getAllCityByProvinceID(selectprovince);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }
    //获取数据库中市的信息，以便快递员可查询

    @RequestMapping(value = "list_working_bills_selector_district.do", method = RequestMethod.POST)
    @ResponseBody
    public Response listWorkingBillsSelector_district(String selectcity, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.getAllDistrictCityID(selectcity);

        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }
    //获取数据库中区的信息，以便快递员可查询

    @RequestMapping(value = "list_working_bills_selector_town.do", method = RequestMethod.POST)
    @ResponseBody
    public Response listWorkingBillsSelector_town(String selectdistrict, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.getAllTownByDistrictID(selectdistrict);

        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    /**
     *
     * @param startweight 重量1
     * @param endweight 重量2
     * @param startfreight 运费1
     * @param endfreight 运费2
     * @param status 状态
     * @param startprovince 开始省份
     * @param endprovince 结束省份
     * @param startcity 开始城市
     * @param endcity 结束城市
     * @param startcreatetime 开始的创建时间
     * @param endcreatetime 结束的创建时间
     * @param startupdatetime 开始的更新时间
     * @param endupdatetime 结束的更新时间
     * @param session Httpsession
     * @return
     */
    @RequestMapping(value = "select_list_working_bills.do", method = RequestMethod.POST)
    @ResponseBody
    public Response selectBillList(double startweight, double endweight, double startfreight, double endfreight,
            String status, String startprovince, String endprovince, String startcity, String endcity,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startcreatetime, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endcreatetime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startupdatetime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endupdatetime, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (Authentication.isExpressLogisticsManager(emp) || Authentication.isExpressCourier(emp) || Authentication.isSuperManager(emp) || Authentication.isExpressAdmin(emp)) {
            int bill_status = -2;
            if (status.equals("001")) {
                bill_status = 2;
            }
            if (status.equals("002")) {
                bill_status = 3;
            }
            if (status.equals("003")) {
                bill_status = 4;
            }

            //前端向后端穿的数据都是json格式的key值
            BillSelector selector = new BillSelector(startweight, endweight, startfreight,
                    endfreight, bill_status, startcreatetime, endcreatetime, startupdatetime, endupdatetime,
                    startprovince, endprovince, startcity, endcity);
            resp = expressService.selectBill(selector);
            return resp;
        }
        return Response.createByErrorMessage("Sorry,You don't have this authority");
    }

    @RequestMapping(value = "save_bill_info.do", method = RequestMethod.POST)
    @ResponseBody
    public Response saveBillInfo(double weight, int amount, double freight, int payway,
            double insurance, String comment, String consignorname, String consignorphone, String consignorprovinceid,
            String consignorcityid, String consignordistrictid, String consignortownid, String consignordressDetails,
            String consigneename, String consigneephone, String consigneeprovinceid, String consigneecityid,
            String consigneedistrictid, String consigneetownid, String consigneedressDetails, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (Authentication.isEmployeeAdmin(emp) || Authentication.isExpressAreaManager(emp)) {
            int bill_status = 0;
            Date date = new Date();
            MapProvince consignorpro = expressService.getProvinceByID(consignorprovinceid);
            MapCity consignorcity = expressService.getCityByID(consignorcityid);
            MapDistrict consignordis = expressService.getDistrictByID(consignordistrictid);
            MapTown consignortown = expressService.getTownByID(consignortownid);
            consignorcity.setMapProvince(consignorpro);
            consignordis.setMapCity(consignorcity);
            consignortown.setMapDistrict(consignordis);
            String consignorID = DataUtil.getUUID();
            CustomerInfo consignorinfo = new CustomerInfo(consignorID, consignortown, consignorname, consignorphone, consignordressDetails, date, date);
            MapProvince consigneepro = expressService.getProvinceByID(consigneeprovinceid);
            MapCity consigneecity = expressService.getCityByID(consigneecityid);
            MapDistrict consigneedis = expressService.getDistrictByID(consigneedistrictid);
            MapTown consigneetown = expressService.getTownByID(consigneetownid);
            consigneecity.setMapProvince(consigneepro);
            consigneedis.setMapCity(consigneecity);
            consigneetown.setMapDistrict(consigneedis);
            String consigneeID = DataUtil.getUUID();
            CustomerInfo consigneeinfo = new CustomerInfo(consigneeID, consigneetown, consigneename, consigneephone, consigneedressDetails, date, date);
            String billID = DataUtil.getUUID();
            expressService.saveCustomerInfo(consignorinfo);
            expressService.saveCustomerInfo(consigneeinfo);
            Bill newbill = new Bill(billID, consignorinfo, consigneeinfo, weight, freight, amount, payway, insurance, comment, bill_status, "", date, date);
            resp = Response.createBySuccess(expressService.addNewBill(newbill));
            return resp;
        }
        return Response.createByErrorMessage("Sorry,You don't have this authority,You cannot create bills");
    }

    //获取billList
    @RequestMapping(value = "list_bills_status0.do", method = RequestMethod.POST)
    @ResponseBody
    public Response getBillListStatus0(int pageNo, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.billForPage(pageNo, 10, 0);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }
    //审批初始订单转成业务单

    @RequestMapping(value = "passStatus0bill.do", method = RequestMethod.POST)
    @ResponseBody
    public Response passStatus0Bill(String billinfoID, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressLogisticsApproval(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.passStatus0Bill(billinfoID);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    //获取业务单
    @RequestMapping(value = "list_bills_status1.do", method = RequestMethod.POST)
    @ResponseBody
    public Response getBillListStatus1(int pageNo, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressAdmin(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.billForPageForNormal(pageNo, 10, 1, emp);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }
    //审批业务单转换成工单

    @RequestMapping(value = "passStatus1bill.do", method = RequestMethod.POST)
    @ResponseBody
    public Response passStatus1Bill(String billinfoID, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressLogisticsApproval(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.passStatus1Bill(billinfoID);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    //异步判断employee的权限
    @RequestMapping(value = "employeeAuthentication.do", method = RequestMethod.POST)
    @ResponseBody
    public Response getEmployeeAuthentication(HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressLogisticsManager(emp) || Authentication.isSuperManager(emp))) {
            resp = Response.createBySuccess("yes");
        } else {
            resp = Response.createBySuccess("no");
        }
        return resp;
    }

    //更新bill表单
    @RequestMapping(value = "update_bill_info.do", method = RequestMethod.POST)
    @ResponseBody
    public Response updateBillForCourier(String billID, double weight, int amount, double freight,
            double insurance, String consignorname, String consignorcity, String consigneename, String consigneecity, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isSuperManager(emp) || Authentication.isExpressLogisticsManager(emp))) {
            Bill bill = expressService.getBillById(billID);
            bill.setAmount(amount);
            bill.setInsured(insurance);
            bill.setWeight(weight);
            bill.setFreight(freight);
            bill.getConsignorInfo().setName(consignorname);
            bill.getConsignorInfo().getMapTown().getMapDistrict().getMapCity().setName(consignorcity);
            bill.getConsigneeInfo().setName(consigneename);
            bill.getConsigneeInfo().getMapTown().getMapDistrict().getMapCity().setName(consigneecity);
            bill.setStatus(3);
            resp = expressService.updateBillInfo(bill);
        } else {
            resp = Response.createBySuccess("no");
        }
        return resp;
    }

    //delete bill表单
    @RequestMapping(value = "deleteoneBill.do", method = RequestMethod.POST)
    @ResponseBody
    public Response deleteBillForCourier(String billID, HttpSession session) {//快递员可以查询的条件集合
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressCourier(emp) || Authentication.isSuperManager(emp) || Authentication.isExpressLogisticsManager(emp))) {
            Bill bill = expressService.getBillById(billID);
            resp = expressService.deleteBill(bill);
        } else {
            resp = Response.createBySuccess("no");
        }
        return resp;
    }

    //条件查询业务单
    @RequestMapping(value = "searchNormalBills.do", method = RequestMethod.POST)
    @ResponseBody
    public Response getBillListStatus1ForAreaManager(@DateTimeFormat(pattern = "yyyy-MM-dd") Date createtimestart, @DateTimeFormat(pattern = "yyyy-MM-dd") Date createtimeend,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date updatetimestart,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date updatetimeend, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressAdmin(emp) || Authentication.isExpressAreaManager(emp))) {
            resp = expressService.billForPageForNormal(1, 10, 1, emp, createtimestart, createtimeend, updatetimestart, updatetimeend);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    @RequestMapping(value = "finishedBill.do", method = RequestMethod.POST)
    @ResponseBody
    public Response finishBill(String billinfoID, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null && (Authentication.isExpressLogisticsApproval(emp) || Authentication.isExpressAdmin(emp))) {
            resp = expressService.finishBill(billinfoID);
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    @RequestMapping(value = "getfinishedBillNum.do", method = RequestMethod.POST)
    @ResponseBody
    public Response getNumOfFinishedBill(HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null) {
            resp = expressService.getfinishedBillNum();
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }

    @RequestMapping(value = "getNotFinishedBillNum.do", method = RequestMethod.POST)
    @ResponseBody
    public Response getNumOfNotFinishedBill(HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (emp != null) {
            resp = expressService.getNotfinishedBillNum();
        } else {
            resp = Response.createByErrorMessage("You are not courier or super VIP");
        }
        return resp;
    }
}
